<template>
	<view>
		<form @submit="onSubmit" @reset="">
			<view class="form-item">
				<text>收件人</text>
				<input type="text" name="name" value="" placeholder="收货人姓名" />
			</view>

			<view class="form-item">
				<text>手机号</text>
				<input type="tel" name="tel" value="" placeholder="收货人手机号" />
			</view>
			
			
			 

			<view class="form-item" @click="chooseArea">
				<text>所在地</text>
				<view class="item-content">				
					<view style="">{{pickerText == '' ? '选择省/市/区(县)' : pickerText}}</view>
				</view>
				<view class="icon"><text class="iconfont icon-ziyuan3"></text></view>
			</view>

			<view class="form-item">
				<text>详情地址</text>
				<input type="text" name="addressDetail" value="" placeholder="街道门牌/楼层房间号等信息" />
			</view>

			<button form-type="submit" class="submit">提交</button>
		</form>

		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor="#007AFF"></simple-address>
	</view>
</template>

<script>
import simpleAddress from '@/components/simple-address/simple-address.vue';



export default {
	components: {
		simpleAddress
	},
	data() {
		return {
			redirect: '',
			cityPickerValueDefault: [0, 0, 1],
			pickerText: '',
			labelArr: ['', '', '']
		};
	},
	onLoad(options) {
		if (options.redirect) {
			this.redirect = options.redirect;
		}
	},
	computed: {},
	methods: {
		backWhere() {
			//如果是从购物车过来的话，则要原路返回购物车

			let redirect = this.redirect;

			if (typeof redirect != 'undefined' && redirect != '') {
				uni.navigateBack({
					url: redirect
				});
			} else {
				uni.navigateBack({
					url: './address'
				});
			}
		},

		onSubmit(e) {
			let content = e.detail.value;

			if (content.name === '') {
				uni.showToast({
					title: '姓名不能为空'
				});
				return false;
			}

			// 省市区
			content.province = this.labelArr[0];
			content.city = this.labelArr[1];
			content.county = this.labelArr[2];
			
			

			this.$http.post('/shop.address/create', content).then(res => {
				if (res.data.data > 0) {
					// Toast('收货地址添加成功');
					this.$EventBus.$emit('address_add_success', 'ok');
					this.backWhere();
					// this.$router.push("/user/address");
				}
			});
		},
		chooseArea() {
			this.cityPickerValueDefault = [0, 0, 1];
			this.$refs.simpleAddress.open();
		},
		onConfirm(e) {
			console.log(JSON.stringify(e));
			this.pickerText = e.label;
			this.labelArr = e.labelArr;
		}
	}
};
</script>

<style lang="scss">

.submit {
	background-color: $colorPrimary;
	color: white;
	border-radius: 0;
	margin-top: 30rpx;
}
</style>
